package com.infitecs.eyas.power.service;

import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.tool.Straight;
import com.infitecs.krill.mybatis.interceptor.PageInterceptor;
import com.infitecs.krill.mybatis.model.Page;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Demon on 2015/4/24.
 */
@Service
public class PowerServiceImp implements PowerService{

    @Autowired
    private PowerDao powerDao;
    @Autowired
    private OperateFieldDao operateFieldDao;


    @Override
    public boolean addPower(Power power) {
        boolean powerCase = powerDao.addPowerCase(power);
        boolean plantCase = powerDao.addPlantCase(power);
        boolean nature = powerDao.addNature(power.getPowerId());
        return powerCase&&plantCase&&nature;
    }

    @Override
    public Power addOnlyPower(Power power) {
        powerDao.addPower(power);
        return power;
    }

    @Override
    public boolean addCaseId(int id) {
        boolean ash = powerDao.addAsh(id);
        boolean ind = powerDao.addIndustry(id);
        boolean ele = powerDao.addElemental(id);
        boolean bur = powerDao.addBurst(id);
        return ash&&ind&&ele&&bur;
    }

    @Override
    public List<Power> getAllPower() {
        return powerDao.getAllPower();
    }

    @Override
    public List<Power> getAllCase(int id) {
        return powerDao.getAllCase(id);
    }

    @Override
    public boolean addCase(Power power) {
        boolean powerCase = powerDao.addPowerCase(power);
        boolean plantCase = powerDao.addPlantCase(power);
        //新增只读修改条件表数据
        operateFieldDao.addOperateField(power.getPowerCaseId());
        return plantCase&&powerCase;
    }

    @Override
    public int getPowerByCaseId(int caseId) {
        return powerDao.getPowerByCaseId(caseId);
    }

    @Override
    public int countPowerByName(Power power) {
        return powerDao.countPowerByName(power);
    }

    @Override
    public int countPowerByCase(Power power) {
        return powerDao.countPowerByCase(power);
    }

    @Override
    public int countPowerCaseName(Power power) {
        return powerDao.countPowerCaseName(power);
    }

    @Override
    public List<Power> getPowerListPage(Page page, Power power) {
        PageInterceptor.startPage(page);
        return powerDao.getPowerListPage(power);
    }

    @Override
    public boolean delCase(String id) {
        int a = powerDao.delPowerCase(id);
        return a == 1;
    }

    @Override
    public List<String> getUserIds(String caseId) {
        List<String> userId = new ArrayList<>();
        String userIds = powerDao.getUserIds(caseId);
        Collections.addAll(userId,userIds.split(","));
        return userId;
    }

    @Override
    public boolean updteShareUserIds(Power power) {
        return powerDao.updateShare(power);
    }

    @Override
    public void editCaseName(Power power) {
        powerDao.editCaseName(power);
        powerDao.editPlantName(power);
    }

}
